﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;//泛类型的引用

/*
  ·Distinct - 过滤集合中的相同项；延迟
    ·Union - 连接不同集合，自动过滤相同项；延迟
    ·Concat - 连接不同集合，不会自动过滤相同项；延迟
    ·Intersect - 获取不同集合的相同项（交集）；延迟
    ·Except - 从某集合中删除其与另一个集合中相同的项；延迟
    ·Skip - 跳过集合的前n个元素；延迟
    ·Take - 获取集合的前n个元素；延迟
    ·SkipWhile - 直到某一条件成立就停止跳过；延迟
    ·TakeWhile - 直到某一条件成立就停止获取；延迟
    ·Single - 根据表达式返回集合中的某一元素；不延迟
    ·SingleOrDefault - 根据表达式返回集合中的某一元素（如果没有则返回默认值）；不延迟
    ·Reverse - 对集合反向排序；延迟
    ·SelectMany - Select选择（一对多）；延迟

 */
public partial class _35newpart_Linq_LINQ查询操作符之Distinct_Union_Concat_Intersect_Except_Skip_Take_SkipWhile_TakeWhile_Single_SingleOrDefault_Reverse_SelectMany : System.Web.UI.Page
{

    Linq查询DataContext LDb = new Linq查询DataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        // Distinct - 过滤集合中的相同项；延迟
        _Summary_Distinct();

        // 连接不同集合，自动过滤相同项；延迟
        _Summary_Union();

        //联接不同集合,不会自动过滤相同项 : ；延迟
        _Summary_Concat();
        //取不同集合的相同项（交集）；延迟
        _Summary_Intersect();

        _Summary_Ecept();

        _Summary_Skip();

        //_Summary_Reverse();

        //_Summary_Single_SingleOrDefault();

        //_Summary_SkipWhile_TakeWhile();

      //  _Summary_SelectMany();

    }

    /**/
    /// <summary>
    /// Distinct - 过滤集合中的相同项；延迟
    /// </summary>
    void _Summary_Distinct()
    {
        var ary = (from p in LDb.books select p).Distinct();

        foreach (var oo in ary)
        {
           Response.Write(oo.bookname + "<br />");
        }
    }
    /**/
    /// <summary>
    /// Union - 连接不同集合，自动过滤相同项；延迟
    /// </summary>
    void _Summary_Union()
    {
        var ary = (from p in LDb.books select p).Take(3).Union((from p in LDb.books select p).Take(6));
        var ary2 = LDb.books.Take(3).Union(LDb.books.Take(8));
        Response.Write("Union - 连接不同集合，自动过滤相同项；延迟<br/><br/><br/><br/><br/>");
        foreach (var oo in ary)
        {
            Response.Write(oo.bookname + "<br />");
        }
    }

    /// <summary>
    /// Concat - 连接不同集合，不会自动过滤相同项；延迟
    /// </summary>
    void _Summary_Concat()
    {
        var ary = (from p in LDb.books select p).Take(3).Concat((from p in LDb.books select p).Take(6));
        var ary2 = LDb.books.Take(3).Concat(LDb.books.Take(8));
        Response.Write("Union - 连接不同集合，自动过滤相同项；延迟<br/><br/><br/><br/><br/>");
        foreach (var oo in ary)
        {
            Response.Write(oo.bookname + "<br />");
        }
    }

   
    /// <summary>
    ///  Intersect - 获取不同集合的相同项（交集）
    /// </summary>
    void _Summary_Intersect()
    {
        var ary = (from p in LDb.books
                   select new { 
                   
                   p = p.bookname,
                   o=p.bookpic
                   }


            ).Take(3).Intersect((
            from p in LDb.books
            select new
            {

                p = p.bookname,
                o = p.bookpic
            }
).Take(6));


        var ary2 = LDb.books.Select(p => new { o = p.bookname, op = p.bookid }).Take(7).Intersect(LDb.books.Take(8).Select(p => new { o = p.bookname, op = p.bookid }));
        Response.Write("Concat - 连接不同集合，不会自动过滤相同项；延迟<br/><br/><br/><br/><br/>");

        foreach (var oo in ary)// SQL Server 未处理 NText、Text、Xml 或 Image 数据类型的比较。 
        {
            Response.Write(oo.o + "<br />");
        }
    }

    //
    /// <summary>
    /// Except - 从某集合中删除其与另一个集合中相同的项；延迟
    /// </summary>
    void _Summary_Ecept()
    {
        var ary = (from p in LDb.books
                   orderby p.bookid descending
                   select new
                   {

                       p = p.bookname,
                       o = p.bookpic
                   }


    ).Take(3).Except((
    from p in LDb.books
    orderby p.bookname descending
    select new
    {

        p = p.bookname,
        o = p.bookpic,
    }
).Take(3));
        var ary2 = LDb.books.Select(p => new { o = p.bookname, op = p.bookid }).Take(3).Except(LDb.books.Take(3).Select(p => new { o = p.bookname, op = p.bookid }));
        Response.Write("<br/><br/><br/><br/><br/>Except - 从某集合中删除其与另一个集合中相同的项；延迟");
        foreach (var oo in ary)
        {
            Response.Write(oo.o + "<br />");
        }
    }


    //Skip - 跳过集合的前n个元素；延迟

    /// <summary>
    /// Skip - 跳过集合的前n个元素；延迟
    /// Take - 获取集合的前n个元素；延迟 
    /// </summary>
    void _Summary_Skip()
    {

        Response.Write("<br/><br/><br/><br/><br/> Skip - 跳过集合的前n个元素；延迟");
        var  ary = LDb.books.Skip(10);

        var ary2 = (from p in LDb.books select p).Skip(20).Take(10);
        foreach (var oo in ary2)
        {
            Response.Write(oo.bookname + "<br />");
        }
    }


    /// <summary>
    /// Single - 根据表达式返回集合中的某一元素；不延迟
    ///SingleOrDefault - 根据表达式返回集合中的某一元素（如果没有则返回默认值）；不延迟
    /// </summary>
    void _Summary_Single_SingleOrDefault()
    {
        var s = (from a in LDb.books
                    select a).Single(a =>a.bookname=="爱可视(Archos)掌上娱乐中心Gmini220");
        // string s = (from a in _ary
        //             select a).SingleOrDefault(a => a == "xxx");
        // s == null

    }

     //   ·SkipWhile - 直到某一条件成立就停止跳过；延迟
    //·TakeWhile - 直到某一条件成立就停止获取；延迟

    void _Summary_SkipWhile_TakeWhile()
    {
        var product = (from p in LDb.books select p).SkipWhile(s => s.bookpic == "").TakeWhile(s => s.bookname.Length > 10);

        Response.Write("TakeWhile - 直到某一条件成立就停止获取；延迟");
        foreach (var oo in product)
        {
            Response.Write(oo.bookname + "<br />");
        }
    }

    //Reverse - 对集合反向排序；延迟
    /// <summary>
    /// Reverse - 对集合反向排序；延迟
    /// </summary>
    void _Summary_Reverse()
    {
        var ary = (from p in LDb.books
                   orderby p.bookid descending
                   select p);


        foreach (var oo in ary)
        {
            Response.Write(oo.bookname + "<br />");
        }
    }

    /// <summary>
    /// SelectMany - Select选择（一对多）；延迟
    /// </summary>
    void _Summary_SelectMany()
    {
        var product = (from p in LDb.books select p).SelectMany(p => p.bookname.Split('A'));

        foreach (var oo in product)
        {
            Response.Write(oo + "<br />");
        }
    }

}
